package grape.attend.rest.classe.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.attend.rest.classe.form.ClasseListForm;
import grape.attend.service.classe.query.ClasseListQuery;
import grape.attend.service.classe.query.ClasseWithNullCondition;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.attend.rest.classe.form.ClasseCreateForm;
import grape.attend.rest.classe.form.ClasseUpdateForm;
import grape.attend.rest.classe.form.ClasseListPageForm;
import grape.attend.rest.classe.vo.ClasseVo;
import grape.attend.rest.classe.mapper.ClasseWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.attend.service.classe.po.Classe;
import grape.attend.service.classe.api.IClasseService;
import java.util.List;
/**
 * <p>
 * 班次设置 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
@RestController
@RequestMapping("/attend/classe")
@Api(tags = "班次设置相关接口")
public class ClasseController extends BaseController<ClasseVo, Classe> {

    @Autowired
    private ClasseWebMapper currentWebMapper;
    @Autowired
    private IClasseService currentService;



     @ApiOperation("添加班次设置")
     @PreAuthorize("hasAuthority('classe:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public ClasseVo create(@RequestBody @Valid ClasseCreateForm cf) {
         Classe po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询班次设置")
     @PreAuthorize("hasAuthority('classe:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public ClasseVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除班次设置")
     @PreAuthorize("hasAuthority('classe:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新班次设置")
     @PreAuthorize("hasAuthority('classe:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public ClasseVo update(@PathVariable String id,@RequestBody @Valid ClasseUpdateForm uf) {
         Classe po = currentWebMapper.formToPo(uf);
         po.setId(id);
         if (isStrEmpty(po.getClockId())) {
             ClasseWithNullCondition classeWithNullCondition = new ClasseWithNullCondition();
             classeWithNullCondition.setPo(po);
             classeWithNullCondition.setClockId(true);
             // 将字典clockId设置为null
             return super.update(classeWithNullCondition);
         }else{
             return super.update(po);
         }
     }

    @ApiOperation("不分页查询班次设置")
    @PreAuthorize("hasAuthority('classe:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<ClasseVo> list(ClasseListForm listForm) {
        Classe po = currentWebMapper.formToPo(listForm);
        ClasseListQuery classeListQuery = new ClasseListQuery();
        classeListQuery.setPoQuery(po);
        return super.list(classeListQuery);
    }

    @ApiOperation("分页查询班次设置")
    @PreAuthorize("hasAuthority('classe:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<ClasseVo> listPage(ClasseListPageForm listPageForm) {
         Classe po = currentWebMapper.formToPo(listPageForm);
        ClasseListQuery classeListQuery = new ClasseListQuery();
        classeListQuery.setPoQuery(po);
        return super.listPage(classeListQuery,listPageForm);
     }

}
